package com.aimuceliang.aimuceliang.ui.activity;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.aimuceliang.aimuceliang.MyApp;
import com.aimuceliang.aimuceliang.bean.HttpResult;
import com.aimuceliang.aimuceliang.dev.R;
import com.aimuceliang.aimuceliang.net.HttpSubscriber;
import com.aimuceliang.aimuceliang.ui.BaseActivity;
import com.aimuceliang.aimuceliang.utils.SharePreferenceUtil;
import com.aimuceliang.aimuceliang.utils.StringUtil;
import com.aimuceliang.aimuceliang.utils.XLog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscription;

/**
 * Created by ziabo on 2017/7/25.
 * 欢迎页
 */

public class SplashActivity extends BaseActivity {

    //@BindView(R.id.iv_bac)
    //ImageView vIvBac;
    @BindView(R.id.vp_first)
    ViewPager vVpFirst;
    @BindView(R.id.start_btn)
    Button startBtn;
    @BindView(R.id.layout_viewpager_point)
    LinearLayout viewPagerPoint;
    private List<ImageView> listImages;
    private String username;
    private String password;

    @Override
    protected int initContentView() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initUIAndListener() {
        //此处需判断是否第一次下载且进入app
        if (!SharePreferenceUtil.getIsFirstInstall(this)) {
            initViewPager();
            SharePreferenceUtil.saveIsFirstInstall(this, true);
        } else {
            vVpFirst.setVisibility(View.GONE);
            //startActivity(MainActivity.class);
            username = SharePreferenceUtil.getUsername(SplashActivity.this);
            password = SharePreferenceUtil.getPassword(SplashActivity.this);
            if (StringUtil.checkStr(username) || StringUtil.checkStr(password)) {
                startActivity(MainActivity.class);
                finish();
            } else {
                startActivity(LoginActivity.class);
                finish();
            }
//            Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
//            vIvBac.setAnimation(animation);
//            animation.setAnimationListener(new Animation.AnimationListener() {
//                @Override
//                public void onAnimationStart(Animation animation) {
//
//                }
//
//                @Override
//                public void onAnimationEnd(Animation animation) {
//                    username = SharePreferenceUtil.getUsername(SplashActivity.this);
//                    password = SharePreferenceUtil.getPassword(SplashActivity.this);
//                    if (StringUtil.checkStr(username) || StringUtil.checkStr(password)) {
//                        startActivity(MainActivity.class);
//                        finish();
//                    } else {
//                        startActivity(LoginActivity.class);
//                        finish();
//                    }
//                }
//
//                @Override
//                public void onAnimationRepeat(Animation animation) {
//
//                }
//            });
        }
    }

    @OnClick({R.id.start_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.start_btn:
                startActivity(LoginActivity.class);
                finish();
                break;
        }
    }

    private void initViewPager() {
        listImages = new ArrayList<>();
        List<Integer> imageIds = new ArrayList<>();
        imageIds.add(R.mipmap.splash_1);
        imageIds.add(R.mipmap.splash_2);
        imageIds.add(R.mipmap.splash_3);
        ImageView[] point = new ImageView[imageIds.size()];
        for (int i = 0; i < imageIds.size(); i++) {
            ImageView iv = new ImageView(this);
            // iv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            iv.setImageResource(imageIds.get(i));
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            listImages.add(iv);
            point[i] = new ImageView(this);
            point[i].setImageResource(R.drawable.banner_point_selector);
            viewPagerPoint.addView(point[i]);
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) point[i].getLayoutParams();
            params.height = LinearLayout.LayoutParams.WRAP_CONTENT;
            params.width = LinearLayout.LayoutParams.WRAP_CONTENT;
            params.setMargins(7, 0, 7, 0);
            point[i].setLayoutParams(params);
            point[0].setSelected(true);
//            if (i == imageIds.size() - 1) {
//                iv.setOnClickListener(v -> {
//                    startActivity(LoginActivity.class);
//                    finish();
//                });
//            }
        }
        PagerAdapter adapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return listImages.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(listImages.get(position));
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(listImages.get(position));
                return listImages.get(position);
            }
        };
        vVpFirst.setAdapter(adapter);
        vVpFirst.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < point.length; i++) {
                    point[i].setSelected(i == position);
                    if (position == listImages.size() - 1) {
                        startBtn.setVisibility(View.VISIBLE);
                    } else {
                        startBtn.setVisibility(View.GONE);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void getData() {
        Subscription s = MyApp.getApiService().login(new HttpSubscriber<HttpResult>() {
            @Override
            public void onFinished() {

            }

            @Override
            public void onNext(HttpResult httpResult) {
                if (httpResult.code.equals("0")) {
                    startActivity(MainActivity.class);
                } else {
                    XLog.d(TAG, httpResult.message);
                    startActivity(LoginActivity.class);
                }
                finish();
            }
        }, username, password);
        addSubscription(s);
    }
}
